{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import numpy as np\n",
    "from network import C3D_model\n",
    "import cv2\n",
    "from collections import Counter\n",
    "import pandas as pd\n",
    "\n",
    "torch.backends.cudnn.benchmark = True\n",
    "list_0 = []\n",
    "list_1 = []\n",
    "\n",
    "def CenterCrop(frame, size):\n",
    "    h, w = np.shape(frame)[0:2]\n",
    "    th, tw = size\n",
    "    x1 = int(round((w - tw) / 2.))\n",
    "    y1 = int(round((h - th) / 2.))\n",
    "\n",
    "    frame = frame[y1:y1 + th, x1:x1 + tw, :]\n",
    "    return np.array(frame).astype(np.uint8)\n",
    "\n",
    "def center_crop(frame):\n",
    "    frame = frame[8:120, 30:142, :]\n",
    "    return np.array(frame).astype(np.uint8)\n",
    "\n",
    "\n",
    "def find_act(video):\n",
    "    device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "    #print(\"Device being used:\", device)\n",
    "\n",
    "    with open('./dataloaders/ucf_labels.txt', 'r') as f:\n",
    "        class_names = f.readlines()\n",
    "        f.close()\n",
    "    # init model\n",
    "    model = C3D_model.C3D(num_classes=101)\n",
    "    checkpoint = torch.load('run\\\\run_0\\\\models\\\\C3D-ucf101_epoch-99.pth.tar', map_location=lambda storage, loc: storage)  #run_0\n",
    "    \n",
    "    \"\"\"\n",
    "    state_dict = model.state_dict()\n",
    "    for k1, k2 in zip(state_dict.keys(), checkpoint.keys()):\n",
    "        state_dict[k1] = checkpoint[k2]\n",
    "    model.load_state_dict(state_dict)\n",
    "    \"\"\"\n",
    "    model.load_state_dict(checkpoint['state_dict'])#模型参数\n",
    "    #optimizer.load_state_dict(checkpoint['opt_dict'])#优化参数\n",
    "    \n",
    "    model.to(device)\n",
    "    model.eval()\n",
    "\n",
    "    # read video\n",
    "    #video = './avidata/UCF-101/ApplyEyeMakeup/v_ApplyEyeMakeup_g01_c06.avi'  这是源代码\n",
    "    cap = cv2.VideoCapture(video)\n",
    "    retaining = True\n",
    "\n",
    "    clip = []\n",
    "    pd_temp = pd.DataFrame()\n",
    "    \n",
    "    while retaining:\n",
    "        retaining, frame = cap.read()\n",
    "        if not retaining and frame is None:\n",
    "            continue\n",
    "        tmp_ = center_crop(cv2.resize(frame, (171, 128)))\n",
    "        tmp = tmp_ - np.array([[[90.0, 98.0, 102.0]]])\n",
    "        clip.append(tmp)\n",
    "        if len(clip) == 16:\n",
    "            inputs = np.array(clip).astype(np.float32)\n",
    "            inputs = np.expand_dims(inputs, axis=0)\n",
    "            inputs = np.transpose(inputs, (0, 4, 1, 2, 3))\n",
    "            inputs = torch.from_numpy(inputs)\n",
    "            inputs = torch.autograd.Variable(inputs, requires_grad=False).to(device)\n",
    "            with torch.no_grad():\n",
    "                outputs = model.forward(inputs)\n",
    "\n",
    "            probs = torch.nn.Softmax(dim=1)(outputs)\n",
    "            label = torch.max(probs, 1)[1].detach().cpu().numpy()[0]\n",
    "            result_0 = class_names[label].split(' ')[-1].strip()\n",
    "            result_1 = \"%.4f\" % probs[0][label]\n",
    "    \n",
    "            list_0.append(result_0)\n",
    "            list_1.append(result_1)\n",
    "            '''\n",
    "            cv2.putText(frame, class_names[label].split(' ')[-1].strip(), (20, 20),\n",
    "                        cv2.FONT_HERSHEY_SIMPLEX, 0.6,\n",
    "                        (0, 0, 255), 1)\n",
    "            cv2.putText(frame, \"prob: %.4f\" % probs[0][label], (20, 40),\n",
    "                        cv2.FONT_HERSHEY_SIMPLEX, 0.6,\n",
    "                        (0, 0, 255), 1)\n",
    "                        # maxlabel = max(a,key=a.count)\n",
    "            '''\n",
    "            clip.pop(0)\n",
    "\n",
    "        #cv2.imshow('result', frame)\n",
    "        cv2.waitKey(30)\n",
    "\n",
    "    pd_temp['label'] = list_0\n",
    "    pd_temp['probs'] = list_1\n",
    "    result_act = max(list_0,key=list_0.count)\n",
    "    pd_temp.columns = ['label','probs'] #注意这个df没有行标签\n",
    "    df_0 = pd_temp.iloc[:,[0,1]][pd_temp[pd_temp.T.index[0]]==result_act] #提取第一列为某值的所有行\n",
    "    result_act_prob =df_0['probs'].astype('float').mean()#某列的平均值\n",
    "    '''\n",
    "    df_2 = pd_temp[~pd_temp['label'].isin([result_act])]  #想找第二多的，还是算了\n",
    "    print(df_2)\n",
    "    '''\n",
    "    cap.release()\n",
    "    #cv2.destroyAllWindows()\n",
    "    return result_act,result_act_prob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "完成第0个视频D:\\crs2021\\video_act_test\\115_1618.mp4识别，结果动作ApplyEyeMakeup的置信度是0.13705897435897435\n",
      "完成第1个视频D:\\crs2021\\video_act_test\\145_2192.mp4识别，结果动作ApplyEyeMakeup的置信度是0.13345161290322582\n",
      "完成第2个视频D:\\crs2021\\video_act_test\\288_4125.mp4识别，结果动作ApplyEyeMakeup的置信度是0.13260387596899226\n",
      "完成第3个视频D:\\crs2021\\video_act_test\\296_4262.mp4识别，结果动作PlayingDhol的置信度是0.1118657142857143\n",
      "完成第4个视频D:\\crs2021\\video_act_test\\42_444.mp4识别，结果动作PlayingDhol的置信度是0.1118657142857143\n",
      "完成第5个视频D:\\crs2021\\video_act_test\\43_461.mp4识别，结果动作PlayingDhol的置信度是0.1118657142857143\n",
      "完成第6个视频D:\\crs2021\\video_act_test\\440_6238.mp4识别，结果动作PlayingDhol的置信度是0.1118657142857143\n",
      "完成第7个视频D:\\crs2021\\video_act_test\\442_6272.mp4识别，结果动作BenchPress的置信度是0.21756166666666668\n",
      "完成第8个视频D:\\crs2021\\video_act_test\\v_ApplyEyeMakeup_g01_c06.avi识别，结果动作BenchPress的置信度是0.21756166666666668\n",
      "完成第9个视频D:\\crs2021\\video_act_test\\v_Biking_g01_c04.avi识别，结果动作PlayingDhol的置信度是0.11649303278688528\n",
      "完成第10个视频D:\\crs2021\\video_act_test\\v_Bowling_g01_c01.avi识别，结果动作PlayingDhol的置信度是0.11649303278688528\n",
      "完成第11个视频D:\\crs2021\\video_act_test\\v_Rafting_g01_c03.avi识别，结果动作PlayingDhol的置信度是0.11649303278688528\n",
      "完成第12个视频D:\\crs2021\\video_act_test\\v_WalkingWithDog_g01_c01.avi识别，结果动作PlayingDhol的置信度是0.11649303278688528\n",
      "成功结束\n"
     ]
    }
   ],
   "source": [
    "num = 0                                                             #——————————————————断后改\n",
    "df_result = pd.DataFrame()\n",
    "for vname in os.listdir(r'D:\\crs2021\\video_act_test'):              #——————————————————改\n",
    "    video = 'D:\\\\crs2021\\\\video_act_test\\\\'+vname\n",
    "    result_act,result_act_prob = find_act(video)\n",
    "    print('完成第{}个视频{}识别，结果动作{}的置信度是{}'.format(num,video,result_act,result_act_prob))\n",
    "\n",
    "    dic_ = [{\n",
    "    'video': vname,    \n",
    "    'label': result_act,\n",
    "    'probs': result_act_prob\n",
    "    }]\n",
    "    df_22 = pd.DataFrame(dic_)\n",
    "    df_result = df_result.append(df_22, ignore_index=True)\n",
    "    df_22 = df_22.drop(df_22.index,inplace=True)\n",
    "    num += 1\n",
    "    if num % 100 == 0:\n",
    "        print('______自动保存第num={}个_______'.format(num))\n",
    "        df_result.to_csv('自动保存actionresult_{}.csv'.format(num), encoding=\"utf-8-sig\")\n",
    "\n",
    "#成功结束自动保存\n",
    "df_result.to_csv('自动保存actionresult_{}_all.csv'.format(num), encoding=\"utf-8-sig\")\n",
    "print('成功结束')    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#任务：  删除后3秒。重新检查是否完整。动作识别怎么提高可靠性。提取关键帧，识别场景。"
   ]
  }
 ]
}